
$identify("org/mathdox/formulaeditor/modules/logic1/or.js");

$require("org/mathdox/parsing/ParserGenerator.js");
$require("org/mathdox/formulaeditor/parsing/openmath/OpenMathParser.js");
$require("org/mathdox/formulaeditor/parsing/expression/ExpressionParser.js");
$require("org/mathdox/formulaeditor/semantics/MultaryOperation.js");
$require("org/mathdox/formulaeditor/semantics/Keyword.js");

$main(function(){

  var symbol = {
    onscreen : "∨",
    openmath : null, // use default with model:cd and model:name
    mathml   : "<mo>∨</mo>"
  };

  /**
   * Define a semantic tree node that represents logic1.or.
   */
  org.mathdox.formulaeditor.semantics.Logic1Or =
    $extend(org.mathdox.formulaeditor.semantics.MultaryOperation, {

      symbol : {

        onscreen : symbol.onscreen,
        openmath : "<OMS cd='logic1' name='or'/>",
        mathml   : symbol.mathml

      },

      precedence : 90

    });
  
  /**
   * Extend the OpenMathParser object with parsing code for logic1.or.
   */
  org.mathdox.formulaeditor.parsing.openmath.OpenMathParser =
    $extend(org.mathdox.formulaeditor.parsing.openmath.OpenMathParser, {

    /**
     * Returns an equality object based on the OpenMath node.
     */
    handleLogic1Or : function(node) {

      // parse the children of the OMA
      var children = node.getChildNodes();
      var operands = [];
      for (var i=1; i<children.length; i++) {
        operands.push(this.handle(children.item(i)));
      }

      // construct the corresponding object
      var result = new org.mathdox.formulaeditor.semantics.Logic1Or();
      result.initialize.apply(result, operands);
      return result;

    }

  });

  org.mathdox.formulaeditor.parsing.openmath.KeywordList["logic1__or"] = new org.mathdox.formulaeditor.semantics.Keyword("logic1", "or", symbol, "infix");

  /**
   * Extend the ExpressionParser object with parsing code for the equality sign.
   */
  var semantics = org.mathdox.formulaeditor.semantics;
  var pG = new org.mathdox.parsing.ParserGenerator();

  if ( "||" == "∨" ) {
    // only one expression, same on screen
    org.mathdox.formulaeditor.parsing.expression.ExpressionParser =
      $extend(org.mathdox.formulaeditor.parsing.expression.ExpressionParser, {

      // expression90 = or | super.expression90
      expression90 : function() {
        var parent = arguments.callee.parent;
        pG.alternation(
          pG.rule("logic1or"),
          parent.expression90).apply(this, arguments);
      },

      // logic1or = 
      //    expression90 "||" expression100
      logic1or :
        pG.transform(
          pG.concatenation(
            pG.rule("expression90"),
            pG.literal("||"),
            pG.rule("expression100")
          ),
          function(result) {
            return new semantics.Logic1Or(result[0], result[2]);
          }
        )

      });
    } else { // allow alternative as displayed on the screen
      org.mathdox.formulaeditor.parsing.expression.ExpressionParser =
        $extend(org.mathdox.formulaeditor.parsing.expression.ExpressionParser, {

        // expression90 = logic1or | 
        //   logic1oralt | super.expression90
        expression90 : function() {
          var parent = arguments.callee.parent;
          pG.alternation(
            pG.rule("logic1or"),
            pG.rule("logic1oralt"),
            parent.expression90).apply(this, arguments);
        },

        // logic1or = 
        //    expression90 "||" expression100
        logic1or :
          pG.transform(
            pG.concatenation(
              pG.rule("expression90"),
              pG.literal("||"),
              pG.rule("expression100")
            ),
            function(result) {
              return new semantics.Logic1Or(result[0], result[2]);
            }
          ),

        // logic1oralt = 
        //    expression90 "∨" expression100
        logic1oralt :
          pG.transform(
            pG.concatenation(
              pG.rule("expression90"),
              pG.literal("∨"),
              pG.rule("expression100")
            ),
            function(result) {
              return new semantics.Logic1Or(result[0], result[2]);
            }
          )

      });
    }

});
